package com.hxrfid.sms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.hxrfid.common.annotation.Log;
import com.hxrfid.common.core.controller.BaseController;
import com.hxrfid.common.core.domain.PageQuery;
import com.hxrfid.common.core.domain.R;
import com.hxrfid.common.core.page.TableDataInfo;
import com.hxrfid.common.core.validate.AddGroup;
import com.hxrfid.common.core.validate.EditGroup;
import com.hxrfid.common.enums.BusinessType;
import com.hxrfid.common.utils.poi.ExcelUtil;
import com.hxrfid.sms.domain.bo.SysSmsConfigBo;
import com.hxrfid.sms.domain.vo.SysSmsConfigVo;
import com.hxrfid.sms.service.ISysSmsConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 短信配置控制器
 * 前端访问路由地址为:/sms/smsConfig
 *
 * @author hxrfid
 * @date 2022-12-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sms/smsConfig")
public class SysSmsConfigController extends BaseController {

    private final ISysSmsConfigService iSysSmsConfigService;

    /**
     * 查询短信配置列表
     */
    @SaCheckPermission("sms:smsConfig:list")
    @GetMapping("/list")
    public TableDataInfo<SysSmsConfigVo> list(SysSmsConfigBo bo, PageQuery pageQuery) {
        return iSysSmsConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出短信配置列表
     */
    @SaCheckPermission("sms:smsConfig:export")
    @Log(title = "短信配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysSmsConfigBo bo, HttpServletResponse response) {
        List<SysSmsConfigVo> list = iSysSmsConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "短信配置", SysSmsConfigVo.class, response);
    }

    /**
     * 获取短信配置详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("sms:smsConfig:query")
    @GetMapping("/{id}")
    public R<SysSmsConfigVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iSysSmsConfigService.queryById(id));
    }

    /**
     * 新增短信配置
     */
    @SaCheckPermission("sms:smsConfig:add")
    @Log(title = "短信配置", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysSmsConfigBo bo) {
        return toAjax(iSysSmsConfigService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改短信配置
     */
    @SaCheckPermission("sms:smsConfig:edit")
    @Log(title = "短信配置", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysSmsConfigBo bo) {
        return toAjax(iSysSmsConfigService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除短信配置
     *
     * @param ids 主键串
     */
    @SaCheckPermission("sms:smsConfig:remove")
    @Log(title = "短信配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iSysSmsConfigService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
